//
//  FacilityView.m
//  MiaoTu
//
//  Created by dllo on 16/9/26.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "FacilityView.h"
#import "FacilityCollectionCell.h"
#import "HotelItem.h"
#import "Resfacilities.h"
#import "FacilityCollectionHeaderView.h"
#import "FacilityCollectionFooterView.h"
#import "SelfSizeTool.h"
#import "UIView+Frame.h"


#define kFacilityCollectionCell @"FacilityCollectionCellIdentifier"
#define kFacilityCollectionHeaderView @"FacilityCollectionHeaderViewIdentifier"
#define kFacilityCollectionFooterView @"FacilityCollectionFooterViewIdentifier"

@interface FacilityView () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation FacilityView

- (void)setHotel:(HotelItem *)hotel {
    _hotel = hotel;
    [_collectionView reloadData];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        UICollectionViewFlowLayout *fl = [[UICollectionViewFlowLayout alloc] init];
        fl.itemSize = CGSizeMake((self.dk_width - 30) / 2.f - 5, 25);
        fl.minimumInteritemSpacing = 5;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(15, 0, self.dk_width - 30, self.dk_height) collectionViewLayout:fl];
        [self addSubview:_collectionView];
        
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.500];
    
    _collectionView.dataSource = self;
    _collectionView.delegate = self;
    _collectionView.backgroundColor = [UIColor clearColor];
    [_collectionView registerNib:[UINib nibWithNibName:@"FacilityCollectionCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kFacilityCollectionCell];
    [_collectionView registerNib:[UINib nibWithNibName:@"FacilityCollectionHeaderView" bundle:[NSBundle mainBundle]] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kFacilityCollectionHeaderView];
    [_collectionView registerNib:[UINib nibWithNibName:@"FacilityCollectionFooterView" bundle:[NSBundle mainBundle]] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kFacilityCollectionFooterView];
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _hotel.ResFacilities.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    FacilityCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kFacilityCollectionCell forIndexPath:indexPath];
    cell.facilityName = _hotel.ResFacilities[indexPath.item].FacilityServicesName;
    return cell;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        FacilityCollectionHeaderView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:kFacilityCollectionHeaderView forIndexPath:indexPath];
        headerView.hotelNameLabel.text = _hotel.ResName;
        return headerView;
    } else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        FacilityCollectionFooterView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:kFacilityCollectionFooterView forIndexPath:indexPath];
        footerView.phoneLabel.text = _hotel.ResPhone;
        footerView.introLabel.text = _hotel.Intro;
        CGFloat introHeight = [SelfSizeTool selfSizeHeightWithString:_hotel.Intro width:_collectionView.dk_width font:[UIFont systemFontOfSize:17.0f]];
        footerView.introLabelHeight.constant = ceilf(introHeight);
        footerView.trafficLabel.text = _hotel.TrafficGuid;
        CGFloat trafficHeight = [SelfSizeTool selfSizeHeightWithString:_hotel.TrafficGuid width:_collectionView.dk_width font:[UIFont systemFontOfSize:17.0f]];
        footerView.trafficLabelHeight.constant = ceilf(trafficHeight);
        [footerView.backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
        UITapGestureRecognizer *tapPhone = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapPhone)];
        [footerView.phoneView addGestureRecognizer:tapPhone];
        return footerView;
    } else {
        return nil;
    }
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(0, 150);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    CGFloat introHeight = [SelfSizeTool selfSizeHeightWithString:_hotel.Intro width:_collectionView.dk_width font:[UIFont systemFontOfSize:17.0f]];
    CGFloat trafficHeight = [SelfSizeTool selfSizeHeightWithString:_hotel.TrafficGuid width:_collectionView.dk_width font:[UIFont systemFontOfSize:17.0f]];
    return CGSizeMake(0, introHeight + trafficHeight + 287);
}

- (void)tapPhone {
    
//    NSString *tel = [NSString stringWithFormat:@"telprompt://%@",_hotel.ResPhone];
//    
//    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:tel]];
    
//    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", _hotel.ResPhone]]];
    
    [[UIApplication sharedApplication]openURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",_hotel.ResPhone]]];
    NSLog(@"phone");
}

- (void)back {
//    [self dismissViewControllerAnimated:YES completion:nil];
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 0;
    }];
}

@end
